package cn.simonfish.carrier.controller.view;

import cn.simonfish.carrier.common.LoginInfo;
import cn.simonfish.carrier.controller.BaseController;
import cn.simonfish.carrier.controller.vo.SiteCoinInfo;
import cn.simonfish.carrier.data.CoinInfo;
import cn.simonfish.carrier.data.DataCrawling;
import cn.simonfish.carrier.data.Site;
import cn.simonfish.carrier.model.SysUserSiteConfig;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.*;

/**
 * Created by simon on 2017/9/10.
 */
public class SubscribeView extends BaseController{


    public void list(){
        prepare_list_data();
        renderFreeMarker("template/subscribe/list.html");
    }

    /**
     * 准备订阅列表的数据
     */
    private void prepare_list_data(){
        LoginInfo loginInfo = getLoginInfo();
        List<Record> list = Db.find("select * from sys_subscribe o where o.user_id=? order by modify_time desc", loginInfo.getUserId());
        List<Record> currList = new ArrayList<>();
        List<Record> expireList = new ArrayList<>();
        long now = System.currentTimeMillis();
        for (Record record:list) {
            if(record.getDate("expire_time").getTime()>now){
                currList.add(record);
            }else{
                expireList.add(record);
            }
        }
        setAttr("currList",currList);
        setAttr("expireList",expireList);
    }


    /**
     * 算出币种数组
     */
    public void over_view(){
        List<Record> coins = new ArrayList<>();
        if(getLoginInfo().getSites().size() > 0) {
            StringBuilder buf = new StringBuilder("SELECT coin,COUNT(WEBSITE) site_count from QUOTE_DATA ");
            buf.append(" where WEBSITE in (");
            for (String site:getLoginInfo().getSites()) {
                buf.append("'").append(site).append("' ,");
            }
            //删除最后一个逗号
            buf.deleteCharAt(buf.length()-1);
            buf.append(")");
            buf.append(" GROUP BY COIN HAVING COUNT(WEBSITE) > 1");
            coins = Db.find(buf.toString());
        }
        setAttr("coins",coins);
        renderFreeMarker("template/subscribe/over_view.html");
    }


    public void data(){
        String coin = getPara("coin");
        if(StrKit.isBlank(coin)){
            renderHtml("非法请求参数:");
            return;
        }
        LoginInfo loginInfo = getLoginInfo();
        Set<String> siteNamesSet = loginInfo.getSites();

        List<SiteCoinInfo> siteCoinList = new ArrayList<>();
        double max_bidprice=0.0,min_askprice=Double.MAX_VALUE;
        for (String siteName  : siteNamesSet) {
            Site site = DataCrawling.getSite(siteName);
            CoinInfo coinInfo = site.get(coin);
            if(coinInfo == null){
                continue;
            }else{
                SiteCoinInfo siteCoinInfo = new SiteCoinInfo();
                siteCoinInfo.setSite(siteName);
                siteCoinInfo.setCoin( coinInfo );

                SysUserSiteConfig conf = loginInfo.getConfigMap().get(siteName);
                if(conf!=null){
                    siteCoinInfo.setRecharge( conf.getInt("recharge_enable")==1);
                    siteCoinInfo.setWithdraw( conf.getInt("withdraw_enable")==1);
                    siteCoinInfo.setDisplay( conf.getInt("display_enable")==1);
                }

                if(siteCoinInfo.isDisplay()==false){
                    continue;
                }
                siteCoinList.add(siteCoinInfo);
                if(siteCoinInfo.isRecharge() ){
                    max_bidprice = Math.max(max_bidprice,coinInfo.getBidprice());
                }
                //是否可以提现
                if(siteCoinInfo.isWithdraw() ){
                    min_askprice = Math.min(min_askprice,coinInfo.getAskprice());
                }
            }

        }
        setAttr("siteCoinList",siteCoinList);
        setAttr("max_bidprice",max_bidprice);
        setAttr("min_askprice",min_askprice);
        setAttr("coin",coin);
        renderFreeMarker("template/subscribe/data.html");

    }


}
